<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新增通知</title>
    <link rel="shortcut icon" href="favicon.ico">
     <link href="<%=path%>/hplus/css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
    <link href="<%=path%>/hplus/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/iCheck/custom.css" rel="stylesheet"> 
    <link href="<%=path%>/hplus/css/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/animate.min.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="<%=path%>/hplus/js/plugins/sweetalert/sweetalert2.min.css">
    <link href="${pageContext.request.contextPath}/hplus/css/plugins/zTree/zTreeStyle/metro.css" rel="stylesheet">
    
    <style> 
        .radio label{
            padding-left: 0;
        }

        .date{
            margin: -8px 20px 0 0;
        }

        .clockpicker{
            margin-top: -8px;
        }

        .i-checks label{
            float: left;
            margin-right: 10px;
        }

        .col-sm-offset-1{
            margin-top: 20px;
        }

        .tishi{
            margin-top: 7px;
            display: inline-block;
        }

        .alert{
            position:absolute;
            top: -15px;
            left: 8%;
            display:none;
        }
        .text-center{
            width:50%;
            margin: 10px 0 20px;
        }

        .radio label::before,.checkbox label::before {
            display: none;
        }
        .form-horizontal .radio{
            display: inline-block;
        }
        .aaa{
            position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0;
        }
        .s_line{
            height: 16px;
            line-height: 16px;
            width: 1px;
            float: left;
            margin: 8px 20px;
            background: #666;
        }
        .red{
            color: red;
            margin-right: 5px;
        }
        
        .ztree li a.curSelectedNode{
		background-color:#BEEBFF;
		}
		
		.ztree li span.button.noline_close{background-position: -126px -84px;}									
	    .ztree li span.button.noline_open{background-position: -105px -84px;}
	
    </style>
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content">
        <div class="ibox-content">
            <form method="get" class="form-horizontal">
                <div class="form-group">
                    <label class="col-sm-2 control-label"><span class="red" style="margin-right:2px">*</span>通知名称：</label>
                    <div class="col-sm-10">
                    <div class="col-sm-3">
                        <input id="noticeName" type="text" maxlength="10" class="form-control" placeholder="请勿超过十个字" >
                    </div>
                    </div>
                </div>                
                
				<!-- 外部应用展示模块 -->
                <div class="form-group">
                <label class="col-sm-2 control-label"><span class="red" style="margin-right:2px">*</span>通知类型：</label>
                <div class="col-sm-10">
                    <div class="col-sm-3" style="display:inline-block;padding-left:14px;padding-top:0;" >
                        <label class="checkbox-inline i-checks">
                            <div class="icheckbox_square-green" style="position: relative;">
                                <input  id="crmCheckId" type="checkbox" value="1" name = "s_yebz" style="position: absolute; opacity: 0;">
                                <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0;"></ins>
                            </div> 短信余额不足
                        </label>
                        <div  style="display:inline-block;padding-top:0;">
                            <button id="crmSetButton" style="display:none;" data-toggle="modal" data-target="#myModa3" type="button" class="btn btn-primary btn-sm k_verify">设置</button>
                        </div>
                    </div>
                </div>
                </div>
                <!-- 通知手机号1 -->
                <div class="form-group">
                    <label class="col-sm-2 control-label"><span class="red" style="margin-right:2px">*</span>通知手机号：</label>
                    <div class="col-sm-10">
                        <div class="col-sm-3" style="display:inline-block;padding-left:14px;padding-top:0;" >
                            <input id="phone1" type="text" maxlength="11" placeholder="请填写手机号" class="form-control" >
                        </div>
                        <div class="col-sm-3 text-left">
                            <button  data-toggle="modal" href="<%=path%>/view/ui/selectUser" data-target="#user_modal" type="button" class="btn btn-primary" style="margin-left:0px;" onclick="selectUser();">选择员工</button>
                            <img id="phoneAdd1" style="cursor:pointer;" src="${pageContext.request.contextPath}/images/system/notice_add.png" onclick="phoneAddFun();">
                        </div>
                    </div>
                </div>
                <!-- 通知手机号2 -->
                <div id="phoneDiv2" class="form-group" style="display: none;">
                    <label class="col-sm-2 control-label"></label>
                    <div class="col-sm-10">
                        <div class="col-sm-3" style="display:inline-block;padding-left:14px;padding-top:0;" >
                            <input id="phone2" type="text" maxlength="11" placeholder="请填写手机号" class="form-control" >
                        </div>
                        <div class="col-sm-3" style="display:inline-block;padding-top:0px;" >
                            <button  data-toggle="modal" href="<%=path%>/view/ui/selectUser" data-target="#user_modal" type="button" class="btn btn-primary" style="margin-left:0px;">选择员工</button>
                            <img id="phoneAdd2" style="cursor:pointer;" src="${pageContext.request.contextPath}/images/system/notice_add.png" onclick="phoneAddFun();">
                            <img id="phoneDelete2" style="cursor:pointer;" src="${pageContext.request.contextPath}/images/system/notice_delete.png" onclick="phoneMinFun('phoneDiv2');">
                        </div>
                    </div>
                </div>
                <!-- 通知手机号3 -->
                <div id="phoneDiv3" class="form-group" style="display: none;">
                    <label class="col-sm-2 control-label"></label>
                    <div class="col-sm-10">
                        <div class="col-sm-3" style="display:inline-block;padding-left:14px;padding-top:0;" >
                            <input id="phone3" type="text" maxlength="11" placeholder="请填写手机号" class="form-control" >
                        </div>
                        <div class="col-sm-3" style="display:inline-block;padding-top:0px;" >
                            <button  data-toggle="modal" href="<%=path%>/view/ui/selectUser" data-target="#user_modal" type="button" class="btn btn-primary" style="margin-left:0px;">选择员工</button>
                            <img id="phoneAdd3" style="cursor:pointer;" src="${pageContext.request.contextPath}/images/system/notice_add.png" onclick="phoneAddFun();">
                            <img id="phoneDelete3" style="cursor:pointer;" src="${pageContext.request.contextPath}/images/system/notice_delete.png" onclick="phoneMinFun('phoneDiv3');">
                        </div>
                    </div>
                </div>
                <!-- 通知手机号4 -->
                <div id="phoneDiv4" class="form-group" style="display: none;">
                    <label class="col-sm-2 control-label"></label>
                    <div class="col-sm-10">
                        <div class="col-sm-3" style="display:inline-block;padding-left:14px;padding-top:0;" >
                            <input id="phone4" type="text" maxlength="11" placeholder="请填写手机号" class="form-control" >
                        </div>
                        <div class="col-sm-3" style="display:inline-block;padding-top:0px;" >
                            <button  data-toggle="modal" href="<%=path%>/view/ui/selectUser" data-target="#user_modal" type="button" class="btn btn-primary" style="margin-left:0px;">选择员工</button>
                            <img id="phoneAdd4" style="cursor:pointer;" src="${pageContext.request.contextPath}/images/system/notice_add.png" onclick="phoneAddFun();">
                            <img id="phoneDelete4" style="cursor:pointer;" src="${pageContext.request.contextPath}/images/system/notice_delete.png" onclick="phoneMinFun('phoneDiv4');">
                        </div>
                    </div>
                </div>
                <!-- 通知手机号5 -->
                <div id="phoneDiv5" class="form-group" style="display: none;">
                    <label class="col-sm-2 control-label"></label>
                    <div class="col-sm-10">
                        <div class="col-sm-3" style="display:inline-block;padding-left:14px;padding-top:0;" >
                            <input id="phone5" type="text" maxlength="11" placeholder="请填写手机号" class="form-control" >
                        </div>
                        <div class="col-sm-3" style="display:inline-block;padding-top:0px;" >
                            <button  data-toggle="modal" href="<%=path%>/view/ui/selectUser" data-target="#user_modal" type="button" class="btn btn-primary" style="margin-left:0px;">选择员工</button>
                            <img id="phoneAdd5" style="cursor:pointer;" src="${pageContext.request.contextPath}/images/system/notice_add.png" onclick="phoneAddFun();">
                            <img id="phoneDelete5" style="cursor:pointer;" src="${pageContext.request.contextPath}/images/system/notice_delete.png" onclick="phoneMinFun('phoneDiv5');">
                        </div>
                    </div>
                </div>


                <div class="form-group">
                    <label class="col-sm-2 control-label">备注：</label>
                    <div class="col-sm-10">
                    <div class="col-sm-5">                      
                        <textarea id="remarks" name="comment" maxlength="500" style="height: 150px;width:500px;" class="form-control" required="" aria-required="true"></textarea>
                    </div>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-12 text-center">
                        <button type="button" class="btn btn-white" data-dismiss="modal" onclick="javascript:window.location.href='<%=path%>/view/ui/smsNotice';">取消</button>
                        <button onclick="javascript:save();" type="button" class="btn btn-primary" style="margin-left:20px;">确定</button>
                    </div>      
                </div>          
            </form>
        </div>    
    </div>


    <!-- 选择员工弹框 -->
    <div  class="modal inmodal fade" id="user_modal" tabindex="-1" role="dialog"  aria-hidden="true" >
        <div class="modal-dialog modal-lg">
            <div class="modal-content" >


            </div>
        </div>
    </div>



    <script src="<%=path%>/hplus/js/jquery.min.js?v=2.1.4"></script>
    <script src="<%=path%>/hplus/js/bootstrap.min.js?v=3.3.6"></script>
    <script src="<%=path%>/hplus/js/content.min.js?v=1.0.0"></script>
    <script src="<%=path%>/hplus/js/plugins/chosen/chosen.jquery.js"></script>
    <script src="<%=path%>/hplus/js/plugins/jsKnob/jquery.knob.js"></script>
    <script src="<%=path%>/hplus/js/plugins/jasny/jasny-bootstrap.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/datapicker/bootstrap-datepicker.js"></script>
    <script src="<%=path%>/hplus/js/plugins/prettyfile/bootstrap-prettyfile.js"></script>    
    <script src="<%=path%>/hplus/js/plugins/switchery/switchery.js"></script>
    <script src="<%=path%>/hplus/js/plugins/ionRangeSlider/ion.rangeSlider.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/iCheck/icheck.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/metisMenu/jquery.metisMenu.js"></script>
    <script src="<%=path%>/hplus/js/plugins/colorpicker/bootstrap-colorpicker.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/clockpicker/clockpicker.js"></script>    
    <script src="<%=path%>/hplus/js/plugins/cropper/cropper.min.js"></script>
    <script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>
    <script src="<%=path%>/hplus/js/plugins/sweetalert/sweetalert2.min.js"></script>
	<script type="text/javascript" src="<%=path%>/js/marketing/dialog.js"></script>
	<script type="text/javascript" src="<%=path%>/js/marketing/public.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
	<script src="<%=path%>/hplus/js/plugins/zTree/jquery.ztree.all-3.5.min.js"></script>
    <script type="text/javascript">

					
    </script>
    
    <script>
	
       //ajax全局同步、此处功能不需要异步
	   $.ajaxSetup({  
	       async : false  
	   });    
    
       //初始化icheck控件
	   $(document).ready(function(){$(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",})});
	
        <!-- 页面自加载方法 -->
        $(function(){
        	//上级传过来用于标记是更新还是新增操作的标识create、edit
            var type = getQueryString("type");
            //进入页面先查询通知数量
            getNoiceNum();
        });
        //通知总数
        var allNotice = 0;
        //所有通知数组
        var allNoticeArr = [];
        //查询有多少个通知
        function getNoiceNum(){
            var url="<%=path%>/smsNotice/ajaxGetAllNotice";
            $.post(url, {
            }, function(data) {
                var realData = data.data;
                if (data.status == 200) {
                    allNotice = realData.length;
                    allNoticeArr = realData;
                }
            });
        }



        //增加手机号文本框的方法
        function phoneAddFun() {
            var p2 = $("#phoneDiv2").is(":hidden");//文本框2是否隐藏
            var p3 = $("#phoneDiv3").is(":hidden");//文本框2是否隐藏
            var p4 = $("#phoneDiv4").is(":hidden");//文本框2是否隐藏
            var p5 = $("#phoneDiv5").is(":hidden");//文本框2是否隐藏
            //如果文本框2隐藏了，那么展示
            if(p2){
                $("#phoneDiv2").show();
                $("#phone2").val('');
                return;
            }else if (p3){
                $("#phoneDiv3").show();
                $("#phone3").val('');
                return;
            }else if (p4){
                $("#phoneDiv4").show();
                $("#phone4").val('');
                return;
            }else if (p5){
                $("#phoneDiv5").show();
                $("#phone5").val('');
                return;
            }else{
                errorDialog("通知任务最多可设置5个手机号");
            }
        }

        //减少手机号文本框的方法,同时清空文本框中的数据
        function phoneMinFun(p) {
            $('#' + p).hide();
            var pInput = p.replace("Div","");
            $('#' + pInput).val('');
        }

        //保存通知
        function save(){


            //通知名称
            var noticeName = $("#noticeName").val();
            if(noticeName == ""){
                errorDialog("请填写通知名称");
                return;
            }

            var noticeType = "";
            $("input[name^='s']").each(function(){//each()方法遍历所有的复选框
                if($(this).is(':checked') == true){//这里就可以判断当前是否被选择了
                    var value = $(this).val();
                    noticeType += value + ",";
                }
            });
            if(noticeType == ""){
                errorDialog("请选择通知类型");
                return;
            }

            if($("#phone1").val() == "" && $("#phone2").val() == "" && $("#phone3").val() == "" && $("#phone4").val() == "" && $("#phone5").val() == ""){
                errorDialog("请填写通知手机号");
                return;
            }
            //正则表达式，判断必须是数字
            var reg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
            if(!reg.test($("#phone1").val()) && $("#phone1").val() !="" ){
                errorDialog("请填写正确手机号");
                return;
            }
            if(!reg.test($("#phone2").val()) && $("#phone2").val() !="" ){
                errorDialog("请填写正确手机号");
                return;
            }
            if(!reg.test($("#phone3").val()) && $("#phone3").val() !="" ){
                errorDialog("请填写正确手机号");
                return;
            }
            if(!reg.test($("#phone4").val()) && $("#phone4").val() !="" ){
                errorDialog("请填写正确手机号");
                return;
            }
            if(!reg.test($("#phone5").val()) && $("#phone5").val() !="" ){
                errorDialog("请填写正确手机号");
                return;
            }

            //通知手机号(按逗号分割)
            var phones = "";
            if($("#phone1").val() != null && $("#phone1").val() != ""){
                phones += $("#phone1").val() + ",";
            }
            if($("#phone2").val() != null && $("#phone2").val() != ""){
                phones += $("#phone2").val() + ",";
            }
            if($("#phone3").val() != null && $("#phone3").val() != ""){
                phones += $("#phone3").val() + ",";
            }
            if($("#phone4").val() != null && $("#phone4").val() != ""){
                phones += $("#phone4").val() + ",";
            }
            if($("#phone5").val() != null && $("#phone5").val() != ""){
                phones += $("#phone5").val() + ",";
            }
            if(phones != ""){
                phones = phones.substring(0,phones.length - 1);
            }
            var strs = phones.split(",");
            if(isRepeat(strs)){
                errorDialog("通知手机号请勿重复使用");
                return;
            }

            var tagNotice = false;
            //发送ajax请求判断通知名称是否重复，如果重复给予用户提示
            var url="<%=path%>/smsNotice/ajaxFindSmsNoticeByType";
            $.post(url, {
                "name":noticeName
            }, function(data) {
                var realData = data.data;
                if (data.status == 200) {
                    //如果通知名称存在，那么提示用户通知不可重复
                    errorDialog("通知名称不可重复");
                }else{
                    if(allNotice > 199){
                        errorDialog("请勿超过200个通知");
                        return;
                    }
                    var smsNoticePhones = [];
                    //判断手机号在其他通知是否存在
                    for(var i=0;i<allNoticeArr.length;i++){
                        var ana = allNoticeArr[i].smsNoticePhone.split(",");
                        for(var o=0;o<ana.length;o++){
                            smsNoticePhones.push(ana[o]);
                        }
                    }
                    var tagCf = false;
                    //循环全部通知，用户选择的通知判断通知手机号是否存在
                    for(var i=0;i<smsNoticePhones.length;i++){
                        for(var o=0;o<strs.length;o++){
                            if(smsNoticePhones[i] == strs[o]){
                                tagCf = true;
                                break;
                            }
                        }
                    }
                    //如果手机号在其他通知存在
                    if(tagCf){
                        errorDialog("通知手机号请勿重复使用");
                        return;
                    }

                    //外部应用
                    if(noticeType != ""){
                        noticeType = noticeType.substring(0,noticeType.length - 1);
                    }

                    //备注
                    var remarks = $("#remarks").val();
                    var url="<%=path%>/smsNotice/ajaxCreateSmsNotice";
                    $.post(url, {
                        "name":noticeName,
                        "smsNoticeTypeId":noticeType,
                        "smsNoticePhone":phones,
                        "remarks":remarks,
                        "smsNoticeNum":0,
                        "smsNoticeNumTotal":0
                    }, function(data) {
                        var realData = data.data;
                        if (data.status == 200) {
                            window.location.href='<%=path%>/view/ui/smsNotice';
                        }else{
                            errorDialog(data.message);
                        }
                    });

                }
            });
        }

        //判断字符串中是重复
       function isRepeat(arr) {
           var hash = {};
           for(var i in arr) {
               if(hash[arr[i]])
               {
                   return true;
               }
               // 不存在该元素，则赋值为true，可以赋任意值，相应的修改if判断条件即可
               hash[arr[i]] = true;
           }
           return false;
       }

       //js数组去重
       Array.prototype.distinct = function (){
           var arr = this,
               len = arr.length;
           arr.sort(function(a,b){  //对数组进行排序才能方便比较
               return a - b;
           })
           function loop(index){
               if(index >= 1){
                   if(arr[index] === arr[index-1]){
                       arr.splice(index,1);
                   }
                   loop(index - 1); //递归loop函数进行去重
               }
           }
           loop(len-1);
           return arr;
       };
       
        
    </script>
    
</body>
</html>

